package com.yunhe.sms.dao.impl;

import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.JdbcUtils;
import com.yunhe.sms.dao.StudentDao;
import com.yunhe.sms.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;


/**
 * Created by Administrator on 2017/9/28 0028.
 */
public class StudentDaoImpl extends BaseDaoImpl<Student> implements StudentDao {

   public Pageable<Student> findList(Pageable<Student> pageable) throws SQLException {
       QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
       String sql = " select id sId,stu_no sNo,stu_name sName,stu_sex sSex,birthday,classz from sys_student limit ?,? ";
      /* String sql = " select * from student limit ?,? ";*/
       List<Student> stuList = qr.query(sql, new BeanListHandler<Student>(Student.class),(pageable.getPage() - 1) * pageable.getSize(), pageable.getSize());

       Integer count = count();

       pageable.setTotal(count);
       pageable.setRows(stuList);

       return pageable;
   }

   public Pageable list(int offset, int size){
       String listSql = " select id sId,stu_no sNo,stu_name sName,stu_sex sSex,birthday,classz from sys_student  ";
       String countSql = " select count(*) from sys_student ";
       Pageable<Student> pageable = super.queryPage(listSql, countSql, offset, size);
       return pageable;

   }






    public static Integer count() throws SQLException {

        String sql = " select count(*) from sys_student ";
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        Long count = (Long) qr.query(sql, new ScalarHandler<>(1));

        return Integer.valueOf(count.toString());
    }


    public static void del(Integer id) throws SQLException {
        String sql = " delete from sys_student where id = ? ";
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        qr.update(sql,id);
    }
    /*public static void save(Stus stus) throws SQLException {

        String sql = " insert into stus(name,sex) values(?,?) ";
        QueryRunner qr = new QueryRunner(JdbcUtils2.getDataSource());
        qr.update(sql,stus.getName(),stus.getSex());
    }
    public static Stus findOne(Integer id) throws SQLException {
        String sql = " select * from stus where id = ?  ";

        QueryRunner qr = new QueryRunner(JdbcUtils2.getDataSource());

        Stus stus = qr.query(sql, new BeanHandler<Stus>(Stus.class), id);
        System.out.println(stus.getName());
        return stus;

    }*/
    public static void main(String[] args) throws SQLException {


    }

    /*public void save(Student stu){
        String sql = " insert into studens(stu_no,stu_name,stu_sex,birthday,classz) values(?,?,?,?,?) ";
        Connection conn = JdbcUtils.getConn();
        PreparedStatement stmt = JdbcUtils.getStmt(conn, sql,stu.getsNo(),stu.getsName(),stu.getsSex(),stu.getBirthday(),stu.getClassz());
        JdbcUtils.exec(stmt);
    }*/
}